var ljLs = {
    dataOptions:{
        //pageNumber:1,
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.zt) {
                    this.zt = this.zt == 'ysp' ? "已审批" : this.zt == 'ytj' ? "已提交" : "已保存";
                }
            })
            return data;
        },
        onClickRow:function (rowIndex, rowData) {
            ljLs.setBtnStatus(rowData);
        },
        onSelect:function (rowIndex, rowData) {
            ljLs.setBtnStatus(rowData);
        },
        toolbar:"#toolbar_gys",
        pagination:true,
        rownumbers:true,
        fitColumns:true,
        singleSelect:true,
        fit:true,
        columns:[
            [
                {field:'lj_mc', title:'路径名称', align: 'center',width:10,formatter:gysCell},
                {field:'lj_jyjg_jc', title:'经营机构', align:'center', width:2},
                {field:'ljGysList', title:'供应商', align:'center', width:12},
                {field:'zt', title:'状态', align:'center', width:2,formatter:formatLjLsSp}
            ]
        ]
    },
    initTree:function () {
        $("#gys_tree_lj").ljtreegrid(1, $("#gys_lj_zggsId").val(), {
            onLoadSuccess:function () {
                $(this).tree("select", $(this).tree("getRoot").target);
                $("#query_gys_lj_jyjg").val($(this).tree("getRoot").id);
                $("#query_gys_lj_jyjg").combotree("setValue", $(this).tree("getRoot").id);
                setTimeout(function () {
                    $("#btn_gys_lj_search").click();
                }, 100);
            },
            onClick:function () {
                $("#query_gys_lj_jyjg").combotree("setValue", $(this).tree("getSelected").id);
                $("#btn_gys_lj_search").click();
            }
        });
    },
    initDatagrid:function () {
        $('#grid_gys_lj').datagrid(ljLs.dataOptions);
    },
    showWindow:function (ljLsdata) {
        var tit = ljLsdata["ljLs.id"] ? "修改供应商" : "添加供应商";
        var win = $.window({
            title:tit,
            iconCls:'icon-add2',
            width:680,
            height:450,
            cache:false,
            url:$.webPath() + "/organ/gys/gys_content",
            data:ljLsdata,
            buttons:[
                {
                    text:'保存',
                    handler:function () {
                        $.messager.progress();
                        $("#lj_gysForm").form('submit', {
                            url:$.webPath() + "/organ/gys/gys_lj_save",
                            onSubmit:function (param) {
                                var syList = $("[name='syList']").val();
                                var xyList = $("[name='xyList']").val();
                                if (syList && xyList) {
                                    $.messager.alert('提示', "只能在一处添加供应商", 'info');
                                    $.messager.progress('close');
                                    return false;
                                }
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    $.messager.progress('close');
                                }
                                return isValid;
                            },
                            success:function (data) {
                                $.messager.progress('close');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(win).window("close");
                                    $('#grid_gys_lj').datagrid('reload');
                                    $('#layout_gys').layout('panel', 'center').panel('refresh');
                                } else
                                    $.messager.alert('提示', json.message, 'info');
                            }
                        });
                    }
                }
            ]
        });
    },
    initBtn:function () {
        //新增
        $("#btn_lj_gys_add").click(function () {
            var data = {"lx":$("#query_gys_lj_lx").val(), "jyjgId":$("#gys_tree_lj").tree("getSelected").id};
            ljLs.showWindow(data);
        });

        //删除
        $("#btn_lj_gys_del").click(function () {
            var row = $('#grid_gys_lj').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否删除此修改供应商路径?', function (r) {
                    if (r) {
                        var index = $('#grid_gys_lj').datagrid('getRowIndex', row);
                        $.post($.webPath() + '/organ/gys/gys_lj_delete', {ljLsId:row.id}, function (data) {
                            data = $.parseJSON(data);
                            if (data.statusCode == '200') {
                                $('#grid_gys_lj').datagrid('deleteRow', index);
                                $('#grid_gys_lj').datagrid('reload');
                                ljLs.setBtnStatus(row);
                            } else {
                                $.messager.alert("提示", data.message, "info");
                            }

                        });
                    }
                });
            } else {
                $.messager.alert("提示", "请选择要删除的记录", "info");
            }
        });
        //提交
        $("#btn_lj_sys_commit").click(function () {
            var row = $('#grid_gys_lj').datagrid('getSelected');
            $.messager.confirm('确认', '是否确认提交此供应商修改记录', function (r) {
                if (r) {
                    $.post($.webPath() + '/organ/gys/gys_lj_commit', {ljLsId:row.id}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#grid_gys_lj').datagrid('reload');
                            $('#layout_gys').layout('panel', 'center').panel('refresh');
                            $.messager.show({title:'提示框', msg:obj.message, timeout:3000, showType:'slide'});
                        } else
                            $.messager.alert('提示', obj.message, 'info');
                    });
                }
            });
        })

        //查询
        $("#btn_gys_lj_search").click(function () {
            $('#grid_gys_lj').datagrid({url:$.webPath() + "/organ/gys/gys_ljwhList", queryParams:ljLs.getQuery()});
        });
    },
    initQuery:function () {
        $("#query_gys_lj_jyjg").ljgsjgtree(1, $("#gys_lj_zggsId").val());
    },
    setBtnStatus:function (row) {
        if (row) {
            if (row.zt == '已保存') {
                $("#btn_lj_gys_add").show();
                $("#btn_lj_sys_commit").show();
                $("#btn_lj_gys_del").show();
            } else if (row.zt == '已提交') {
                $("#btn_lj_gys_add").show();
                $("#btn_lj_sys_commit").hide();
                $("#btn_lj_gys_del").show();
            } else if (row.zt == '已审批') {
                $("#btn_lj_gys_add").show();
                $("#btn_lj_sys_commit").hide();
                $("#btn_lj_gys_del").hide();
            }
        }
    },
    alwayShowDetail:function (rowData) {
        $('#layout_gys').layout('remove', 'center');
        $('#layout_gys').layout('add', {
            region:'center',
            split:true,
            href:$.webPath() + '/organ/gys/gys_detail?ljLsId=' + rowData.id
        });
    },
    showDetail:function (rowData) {
        if (rowData && $("#ljLs_detail_id").val() != rowData.id) {
            ljLs.alwayShowDetail(rowData);
        } else {
            $('#layout_gys').layout('remove', 'center');
        }
    },
    getQuery:function () {
        return {
            "jyjgId":$("#query_gys_lj_jyjg").combotree("tree").tree("getSelected").id,
            "lx":$("#query_gys_lj_lx").val()
        }
    }
}

$(function () {
    //树-加载
    ljLs.initTree();
    //列表-加载
    ljLs.initDatagrid();
    //按钮事件-加载
    ljLs.initBtn();
    //按钮状态
    ljLs.setBtnStatus();
    //查询条件-加载
    ljLs.initQuery();
});


function formatLjLsSp(val, row) {
    if (!row.processInstanceId) {
        return val;
    } else {
        return '<a href="javascript:viewLjLsFlowPicture();" style="color: red">' + val + '</a>';
    }
}

function viewLjLsFlowPicture() {
    var row = $('#grid_gys_lj').datagrid('getSelected');
    $.window({
        title:"流程历史",
        iconCls:'icon-add2',
        url:$.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(row.processInstanceId)) + "&businessNo=" + row.mc,
        width:650,
        height:500,
        closed:true,
        modal:true,
        isMax:true
    });
}
